/**
 * @version 2007-5-9
 * @author xalinx at gmail dot com
 */
package org.nanhill.support.spring.dbutil;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;

/**
 * @version 2007-5-9
 * @author xalinx at gmail dot com
 * 
 */
public class DaoTemplate extends JdbcTemplate {

	public DaoTemplate(DataSource dataSource) {
		super(dataSource);
	}

	@Override
	public Object queryForObject(String sql, Object[] args, RowMapper rowMapper) throws DataAccessException {
		List results = (List) query(sql, args, new RowMapperResultSetExtractor(rowMapper, 1));
		if (results.isEmpty()) {
			return null;
		} else {
			return results.iterator().next();
		}
	}

	@Override
	public Object queryForObject(String sql, RowMapper rowMapper) throws DataAccessException {
		List results = query(sql, rowMapper);
		if (results.isEmpty()) {
			return null;
		} else {
			return results.iterator().next();
		}
	}

}
